/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark.GameElements;

using BlindShark.Widgets;

namespace BlindShark.GameStates
{
	/// <summary>
	/// Displays the players, and allows the selection of one of them
	/// </summary>
	public class GS_ConfigurationTracklist : GameState
	{	
		const string CHECKBOX_FILE = "gfx/checkBoxes.jpg";
		
		#region Variables
		// Previous Gamestate
		GameState _gsPrevious;
		
		// Surfaces
		Surface _sfFrame;
						
		// Texts
		string _sFont;
		
		// Elements location
		Rectangle _rctFrame;
		
		// Buttons
		Button _btnConfigTracklist;
		Button _btnConfigGame;
		Button _btnConfigChallenge;
		Button _btnValidate;
		Button _btnCancel;
		
		// Checkboxes
		CheckBox _chkRandomizeNever;
		CheckBox _chkRandomizeAlways;
		CheckBox _chkRandomizeTracklist;
		CheckBox _chkClueYes;
		CheckBox _chkClueNo;
		CheckBox _chkStartBeginning;
		CheckBox _chkStartMiddle;
		
		// Labels
		Label _lblTitle;
		Label _lblPropositions;
		Label _lblCategories;
		
		// TextBoxes
		TextBox _txtClueDelay;
		TextBox _txtStartLimitMin;
		TextBox _txtStartLimitMax;
		
		#endregion
		
		#region Constructors
		public GS_ConfigurationTracklist(GameState previous)
		{
			_gsPrevious = previous;
			
			// Initializing the fonts used
			_sFont = "fonts/Fertigo_17b.otf";
			Label.DefaultFont = _sFont;
			
			// Initializing the labels
			// Title Label
			_lblTitle = new Label(LanguageManager.Get("CFT_Title"), 36, Color.OrangeRed, TextAlignment.TopMiddle);
			_lblTitle.Underline = true;
			// Propositions Label
			_lblPropositions = new Label("", 20, Color.Orange, TextAlignment.CenterLeft);
			// Categories Label
			_lblCategories = new Label("", 22, Color.Orange, TextAlignment.CenterRight);
			
			// Initializing the buttons
			// Tracklist Tab
			_btnConfigTracklist = new Button("gfx/buttons2.jpg", false);
			_btnConfigTracklist.Enabled = false;
			_btnConfigTracklist.Font = _sFont;
			_btnConfigTracklist.TextColor = Color.SaddleBrown;
			_btnConfigTracklist.TextSize = 20;
			_btnConfigTracklist.Text = LanguageManager.Get("CF_Tracklist");
			// Game Tab
			_btnConfigGame = new Button("gfx/buttons1.jpg", true);
			_btnConfigGame.Click += new Button.ClickEvent(ChangeToGame);
			_btnConfigGame.Font = _sFont;
			_btnConfigGame.TextColor = Color.Orange;
			_btnConfigGame.TextSize = 20;
			_btnConfigGame.Text = LanguageManager.Get("CF_Game");
			// Challenge Tab
			_btnConfigChallenge = new Button("gfx/buttons1.jpg", true);
			_btnConfigChallenge.Click += new Button.ClickEvent(ChangeToChallenge);
			_btnConfigChallenge.Font = _sFont;
			_btnConfigChallenge.TextColor = Color.Orange;
			_btnConfigChallenge.TextSize = 20;
			_btnConfigChallenge.Text = LanguageManager.Get("CF_Challenge");
			// Validation Button
			_btnValidate = new Button("gfx/buttons1.jpg", true);
			_btnValidate.Click += new Button.ClickEvent(Validate);
			_btnValidate.Font = _sFont;
			_btnValidate.TextColor = Color.Orange;
			_btnValidate.TextSize = 20;
			_btnValidate.Text = LanguageManager.Get("CF_Validate");
			// Validation Button
			_btnCancel = new Button("gfx/buttons1.jpg", true);
			_btnCancel.Click += new Button.ClickEvent(Cancel);
			_btnCancel.Font = _sFont;
			_btnCancel.TextColor = Color.Orange;
			_btnCancel.TextSize = 20;
			_btnCancel.Text = LanguageManager.Get("CF_Cancel");
						
			// Initializing the Checkboxes
			// Never Randomize
			_chkRandomizeNever = new CheckBox(CHECKBOX_FILE);
			_chkRandomizeNever.Checked = GameManager.RandomizedList == GameManager.RandomChoice.Never;
			_chkRandomizeNever.Enabled = !_chkRandomizeNever.Checked;
			_chkRandomizeNever.ChangeValue += new CheckBox.ChangeValueEvent(ChangeValRandomizeNever);
			// Always Randomize
			_chkRandomizeAlways = new CheckBox(CHECKBOX_FILE);
			_chkRandomizeAlways.Checked = GameManager.RandomizedList == GameManager.RandomChoice.Always;
			_chkRandomizeAlways.Enabled = !_chkRandomizeAlways.Checked;
			_chkRandomizeAlways.ChangeValue += new CheckBox.ChangeValueEvent(ChangeValRandomizeAlways);
			// Randomize Depending Tracklist
			_chkRandomizeTracklist = new CheckBox(CHECKBOX_FILE);
			_chkRandomizeTracklist.Checked = GameManager.RandomizedList == GameManager.RandomChoice.TracklistChoice;
			_chkRandomizeTracklist.Enabled = !_chkRandomizeTracklist.Checked;
			_chkRandomizeTracklist.ChangeValue += new CheckBox.ChangeValueEvent(ChangeValRandomizeTracklist);
			// Don't show Clues
			_chkClueNo = new CheckBox(CHECKBOX_FILE);
			_chkClueNo.Checked = GameManager.ClueDisplayChoice == GameManager.ClueDisplay.Never;
			_chkClueNo.Enabled = !_chkClueNo.Checked;
			_chkClueNo.ChangeValue += new CheckBox.ChangeValueEvent(ChangeValClueNo);
			// Show Clues
			_chkClueYes = new CheckBox(CHECKBOX_FILE);
			_chkClueYes.Checked = GameManager.ClueDisplayChoice != GameManager.ClueDisplay.Never; // TODO : change when AfterFails works
			_chkClueYes.Enabled = !_chkClueYes.Checked;
			_chkClueYes.ChangeValue += new CheckBox.ChangeValueEvent(ChangeValClueYes);
			// Start at Beginning
			_chkStartBeginning = new CheckBox(CHECKBOX_FILE);
			_chkStartBeginning.Checked = !GameManager.RandomStart;
			_chkStartBeginning.Enabled = !_chkStartBeginning.Checked;
			_chkStartBeginning.ChangeValue += new CheckBox.ChangeValueEvent(ChangeValStartBeginning);
			// Start in Middle
			_chkStartMiddle = new CheckBox(CHECKBOX_FILE);
			_chkStartMiddle.Checked = GameManager.RandomStart;
			_chkStartMiddle.Enabled = !_chkStartMiddle.Checked;
			_chkStartMiddle.ChangeValue += new CheckBox.ChangeValueEvent(ChangeValStartMiddle);
			
			// Initializing the TextBoxes
			// Delay before displaying the Clue
			_txtClueDelay = new TextBox();
			_txtClueDelay.Size = new Size(32, 26);
			_txtClueDelay.FontFile = _sFont;
			_txtClueDelay.FontSize = 18;
			_txtClueDelay.OnlyNumeric = true;
			_txtClueDelay.MaxLength = 2;
			_txtClueDelay.Text = GameManager.ClueDisplayValue.ToString();
			// Minimum Limit for a random start
			_txtStartLimitMin = new TextBox();
			_txtStartLimitMin.Size = new Size(32, 26);
			_txtStartLimitMin.FontFile = _sFont;
			_txtStartLimitMin.FontSize = 18;
			_txtStartLimitMin.OnlyNumeric = true;
			_txtStartLimitMin.MaxLength = 2;
			_txtStartLimitMin.Text = GameManager.RndStartLimitMin.ToString();
			// Maximum Limit for a random start
			_txtStartLimitMax = new TextBox();
			_txtStartLimitMax.Size = new Size(32, 26);
			_txtStartLimitMax.FontFile = _sFont;
			_txtStartLimitMax.FontSize = 18;
			_txtStartLimitMax.OnlyNumeric = true;
			_txtStartLimitMax.MaxLength = 2;
			_txtStartLimitMax.Text = GameManager.RndStartLimitMax.ToString();
		}
		#endregion
		
		#region GameState interface members
		public void Draw()
		{
			// Displaying the Frame
			ScreenManager.MainScreen.Blit(_sfFrame, _rctFrame.Location);
			
			// Displaying the Tab Buttons
			ScreenManager.MainScreen.Blit(_btnConfigTracklist.Surface, _btnConfigTracklist.Location);
			ScreenManager.MainScreen.Blit(_btnConfigGame.Surface, _btnConfigGame.Location);
			ScreenManager.MainScreen.Blit(_btnConfigChallenge.Surface, _btnConfigChallenge.Location);
			ScreenManager.MainScreen.Blit(_btnValidate.Surface, _btnValidate.Location);
			ScreenManager.MainScreen.Blit(_btnCancel.Surface, _btnCancel.Location);
			
			// Displaying the Checkboxes
			ScreenManager.MainScreen.Blit(_chkRandomizeNever.Surface, _chkRandomizeNever.Location);
			ScreenManager.MainScreen.Blit(_chkRandomizeAlways.Surface, _chkRandomizeAlways.Location);
			ScreenManager.MainScreen.Blit(_chkRandomizeTracklist.Surface, _chkRandomizeTracklist.Location);
			ScreenManager.MainScreen.Blit(_chkClueNo.Surface, _chkClueNo.Location);
			ScreenManager.MainScreen.Blit(_chkClueYes.Surface, _chkClueYes.Location);
			ScreenManager.MainScreen.Blit(_chkStartBeginning.Surface, _chkStartBeginning.Location);
			ScreenManager.MainScreen.Blit(_chkStartMiddle.Surface, _chkStartMiddle.Location);
			
			// Displaying the TextBoxes
			ScreenManager.MainScreen.Blit(_txtClueDelay.Surface, _txtClueDelay.Location);
			ScreenManager.MainScreen.Blit(_txtStartLimitMin.Surface, _txtStartLimitMin.Location);
			ScreenManager.MainScreen.Blit(_txtStartLimitMax.Surface, _txtStartLimitMax.Location);
		}
		
		public void CleanUp()
		{
			_sfFrame = null;
			_btnConfigTracklist.CleanUp();
			_btnConfigGame.CleanUp();
			_btnConfigChallenge.CleanUp();
			_btnValidate.CleanUp();
			_btnCancel.CleanUp();
			_chkRandomizeNever.CleanUp();
			_chkRandomizeAlways.CleanUp();
			_chkRandomizeTracklist.CleanUp();
			_chkClueNo.CleanUp();
			_chkClueYes.CleanUp();
			_chkStartBeginning.CleanUp();
			_chkStartMiddle.CleanUp();
			_txtClueDelay.CleanUp();
			_txtStartLimitMin.CleanUp();
			_txtStartLimitMax.CleanUp();
			
			Events.Remove();
		}
		
		public void HandleEvents()
		{
			while (Events.Poll())
			{
				// Handling the buttons events
				_btnConfigTracklist.HandleEvents();
				_btnConfigGame.HandleEvents();
				_btnConfigChallenge.HandleEvents();
				_btnValidate.HandleEvents();
				_btnCancel.HandleEvents();
				
				// Handling the Checkboxes Events
				_chkRandomizeNever.HandleEvents();
				_chkRandomizeAlways.HandleEvents();
				_chkRandomizeTracklist.HandleEvents();
				_chkClueNo.HandleEvents();
				_chkClueYes.HandleEvents();
				_chkStartBeginning.HandleEvents();
				_chkStartMiddle.HandleEvents();
				
				// Handling the Textboxes Events
				_txtClueDelay.HandleEvents();
				_txtStartLimitMin.HandleEvents();
				_txtStartLimitMax.HandleEvents();
			}
		}
		
		public void Update()
		{
			
		}
		
		public void Init()
		{
			// Displaying the background one for once
			ScreenManager.MainScreen.Blit(SurfaceManager.GetSurface("gfx/background.png"));
			
			// Initializing the frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoWindow.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoWindow.png"));
			_rctFrame = new Rectangle(new Point(100,50),_sfFrame.Size);
			
			// Initializing the title location
			_lblTitle.Location = new Point(0, 15);
			_lblTitle.Size = _rctFrame.Size;
			_lblTitle.Init();
			_sfFrame.Blit(_lblTitle.Surface, _lblTitle.Location);
			
			// Initializing the buttons
			// Tracklist Tab
			_btnConfigTracklist.Init();
			_btnConfigTracklist.X = _rctFrame.Right + 10;
			_btnConfigTracklist.Y = _rctFrame.Top + 40;
			// Game Tab
			_btnConfigGame.Init();
			_btnConfigGame.X = _rctFrame.Right + 10;
			_btnConfigGame.Y = _btnConfigTracklist.Bottom + 10;
			// Challenge Tab
			_btnConfigChallenge.Init();
			_btnConfigChallenge.X = _rctFrame.Right + 10;
			_btnConfigChallenge.Y = _btnConfigGame.Bottom + 10;
			// Validate Button
			_btnValidate.Init();
			_btnValidate.X = _rctFrame.Right - 10 - _btnValidate.Width;
			_btnValidate.Y = _rctFrame.Bottom - 10 - _btnValidate.Height;
			// Cancel Button
			_btnCancel.Init();
			_btnCancel.X = _rctFrame.Left + 10;
			_btnCancel.Y = _rctFrame.Bottom - 10 - _btnCancel.Height;
			
			// Initializing the categories' Label
			_lblCategories.X = 15;
			_lblCategories.Size = new Size(280, 32);
			// Initializing the propositions' Label
			_lblPropositions.Size = new Size(65, 32);
			
			
			// Category : Randomize the Tracks
			SetCategory(70, LanguageManager.Get("CFT_RandomizeTracks") + " :");
			// Initializing the Checkbox "Never Randomize"
			_chkRandomizeNever.Init();
			_chkRandomizeNever.Location = new Point(_rctFrame.X + _lblCategories.Right + 10,
			                                        _rctFrame.Y + _lblCategories.Y);
			SetProposition(_chkRandomizeNever.Right - _rctFrame.X, _chkRandomizeNever.Y - _rctFrame.Y, LanguageManager.Get("CFT_RandomizeNever"));
			// Initializing the Checkbox "Always Randomize"
			_chkRandomizeAlways.Init();
			_chkRandomizeAlways.Location = new Point(_chkRandomizeNever.X, _chkRandomizeNever.Bottom);
			SetProposition(_chkRandomizeAlways.Right - _rctFrame.X, _chkRandomizeAlways.Y - _rctFrame.Y, LanguageManager.Get("CFT_RandomizeAlways"));
			// Initializing the Checkbox "Randomize Depending Tracklist"
			_chkRandomizeTracklist.Init();
			_chkRandomizeTracklist.Location = new Point(_chkRandomizeAlways.X, _chkRandomizeAlways.Bottom);
			SetProposition(_chkRandomizeTracklist.Right - _rctFrame.X, _chkRandomizeTracklist.Y - _rctFrame.Y, LanguageManager.Get("CFT_RandomizeDepending"));
						
			
			// Category : Clue Display
			SetCategory(_lblCategories.Y + _lblCategories.Height * 4 + 10, LanguageManager.Get("CFT_ClueDisplay") + " :");
			// Initializing the Checkbox "Clue Display : No"
			_chkClueNo.Init();
			_chkClueNo.Location = new Point(_rctFrame.X + _lblCategories.Right + 10,
			                                _rctFrame.Y + _lblCategories.Y);
			SetProposition(_chkClueNo.Right - _rctFrame.X, _chkClueNo.Y - _rctFrame.Y, LanguageManager.Get("CFT_ClueDisplayNo"));
			// Initializing the Checkbox "Clye Display : Yes"
			_chkClueYes.Init();
			_chkClueYes.Location = new Point(_rctFrame.X + _lblPropositions.Right,
			                                 _rctFrame.Y + _lblPropositions.Y);
			SetProposition(_chkClueYes.Right - _rctFrame.X, _chkClueYes.Y - _rctFrame.Y, LanguageManager.Get("CFT_ClueDisplayYes"));
			
			// Category : Clue Value
			SetCategory(_lblCategories.Y + _lblCategories.Height, LanguageManager.Get("CFT_ClueValue") + " :");
			_txtClueDelay.Init();
			_txtClueDelay.Location = new Point(_rctFrame.X + _lblCategories.Right + 10,
			                                   _rctFrame.Y + _lblCategories.Y + 3);
			SetProposition(_txtClueDelay.Right - _rctFrame.X + 5, _lblCategories.Y, LanguageManager.Get("CFT_ClueValueUnit"));
			
			// Category : Tracks Beginning
			SetCategory(_lblCategories.Y + _lblCategories.Height * 2 + 10, LanguageManager.Get("CFT_RandomStart") + " :");
			// Initializing the Checkbox "Start at Beginning"
			_chkStartBeginning.Init();
			_chkStartBeginning.Location = new Point(_rctFrame.X + _lblCategories.Right + 10,
			                                        _rctFrame.Y + _lblCategories.Y);
			SetProposition(_chkStartBeginning.Right - _rctFrame.X, _chkStartBeginning.Y - _rctFrame.Y, LanguageManager.Get("CFT_RandomStartBeginning"));
			// Initializing the Checkbox "Start in Middle"
			_chkStartMiddle.Init();
			_chkStartMiddle.Location = new Point(_rctFrame.X + _lblPropositions.Right,
			                                     _rctFrame.Y + _lblPropositions.Y);
			SetProposition(_chkStartMiddle.Right - _rctFrame.X, _chkStartMiddle.Y - _rctFrame.Y, LanguageManager.Get("CFT_RandomStartMiddle"));
			
			// Category : setting the limits of a Random Start
			SetCategory(_lblCategories.Y + _lblCategories.Height, LanguageManager.Get("CFT_RandomStartLimits") + " :");
			_txtStartLimitMin.Init();
			_txtStartLimitMin.Location = new Point(_rctFrame.X + _lblCategories.Right + 10,
			                                       _rctFrame.Y + _lblCategories.Y + 3);
			SetProposition(_txtStartLimitMin.Right - _rctFrame.X + 5, _lblCategories.Y, LanguageManager.Get("CFT_RandomStartLimitMin"));
			_txtStartLimitMax.Init();
			_txtStartLimitMax.Location = new Point(_rctFrame.X + _lblPropositions.Right,
			                                       _rctFrame.Y + _lblPropositions.Y + 3);
			SetProposition(_txtStartLimitMax.Right - _rctFrame.X + 5, _lblCategories.Y, LanguageManager.Get("CFT_RandomStartLimitMax"));
			
			
		}
		#endregion
	
		#region Buttons Events
		private void SaveConfig()
		{
			if (_chkRandomizeNever.Checked)
				GameManager.RandomizedList = GameManager.RandomChoice.Never;
			else if (_chkRandomizeAlways.Checked)
				GameManager.RandomizedList = GameManager.RandomChoice.Always;
			else
				GameManager.RandomizedList = GameManager.RandomChoice.TracklistChoice;
			
			GameManager.ClueDisplayValue = Convert.ToInt32(_txtClueDelay.Text);
			GameManager.ClueDisplayChoice = _chkClueYes.Checked ? 
				(GameManager.ClueDisplayValue != 0 ? GameManager.ClueDisplay.AfterTime : GameManager.ClueDisplay.Always) :
				GameManager.ClueDisplay.Never;
			
			GameManager.RandomStart = _chkStartMiddle.Checked;
			GameManager.RndStartLimitMin = Convert.ToInt32(_txtStartLimitMin.Text);
			GameManager.RndStartLimitMax = Convert.ToInt32(_txtStartLimitMax.Text);
		}
		
		public void ChangeToGame()
		{
			SaveConfig();
			GameStateManager.ChangeState(new GS_ConfigurationGame(_gsPrevious));
		}
		
		public void ChangeToChallenge()
		{
			SaveConfig();
			GameStateManager.ChangeState(new GS_ConfigurationChallenge(_gsPrevious));
		}
		
		public void Validate()
		{
			SaveConfig();
			
			// Saving and "jumping" to the previous state
			GameManager.SaveConfiguration();
			GameStateManager.ChangeState(new GS_MenuMain());
		}
		
		public void Cancel()
		{
			// Getting back the old configuration, and "jumping" to the previous state
			GameManager.LoadConfiguration();
			GameStateManager.ChangeState(_gsPrevious);
		}
		#endregion
		
		#region Configuring Labels
		private void SetCategory(int y, string text)
		{
			_lblCategories.Y = y;
			_lblCategories.Text = text;
			_lblCategories.Init();
			_sfFrame.Blit(_lblCategories.Surface, _lblCategories.Location);
		}
		
		private void SetProposition(int x, int y, string text)
		{
			_lblPropositions.Location = new Point(x, y);
			_lblPropositions.Text = text;
			_lblPropositions.Init();
			_sfFrame.Blit(_lblPropositions.Surface, _lblPropositions.Location);
		}
		#endregion
		
		#region CheckBoxes Events
		public void ChangeValRandomizeNever()
		{
			// Disable-Enable the checkboxes
			_chkRandomizeNever.Enabled = false;
			_chkRandomizeAlways.Enabled = true;
			_chkRandomizeTracklist.Enabled = true;
			
			// De-checking other checkboxes
			_chkRandomizeAlways.Checked = false;
			_chkRandomizeTracklist.Checked = false;
		}
		
		public void ChangeValRandomizeAlways()
		{
			// Disable-Enable the checkboxes
			_chkRandomizeAlways.Enabled = false;
			_chkRandomizeNever.Enabled = true;
			_chkRandomizeTracklist.Enabled = true;
			
			// De-checking other checkboxes
			_chkRandomizeNever.Checked = false;
			_chkRandomizeTracklist.Checked = false;
		}
		
		public void ChangeValRandomizeTracklist()
		{
			// Disable-Enable the checkboxes
			_chkRandomizeTracklist.Enabled = false;
			_chkRandomizeNever.Enabled = true;
			_chkRandomizeAlways.Enabled = true;
			
			// De-checking other checkboxes
			_chkRandomizeNever.Checked = false;
			_chkRandomizeAlways.Checked = false;
		}
		
		public void ChangeValClueNo()
		{
			// Disable-Enable the checkboxes
			_chkClueNo.Enabled = false;
			_chkClueYes.Enabled = true;
			
			// De-checking other checkboxes
			_chkClueYes.Checked = false;
		}
		
		public void ChangeValClueYes()
		{
			// Disable-Enable the checkboxes
			_chkClueYes.Enabled = false;
			_chkClueNo.Enabled = true;
			
			// De-checking other checkboxes
			_chkClueNo.Checked = false;
		}
		
		public void ChangeValStartBeginning()
		{
			// Disable-Enable the checkboxes
			_chkStartBeginning.Enabled = false;
			_chkStartMiddle.Enabled = true;
			
			// De-checking other checkboxes
			_chkStartMiddle.Checked = false;
		}
		
		public void ChangeValStartMiddle()
		{
			// Disable-Enable the checkboxes
			_chkStartMiddle.Enabled = false;
			_chkStartBeginning.Enabled = true;
			
			// De-checking other checkboxes
			_chkStartBeginning.Checked = false;
		}
		#endregion
	}
}

